<template>
  <div class="global-container">
   <div class="login-box">
       <!-- 头像区域 -->
       <div class="avatar-box">
           <img src="../assets/one.png" alt="">
       </div>
       <!-- 表单区域 -->
       <!-- :model="form"值类型是一个对象（存储整个表单的数据） -->
       <el-form ref="form" class="login_form" :model="form" :rules="rules">
        <el-form-item prop="username">
            <el-input v-model="form.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
            <el-input type="password" v-model="form.password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <el-form-item class="btns">
            <el-button type="primary" @click="login">登陆</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
   </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  created () {

  },

  methods: {
    resetForm () {
      // console.log(this.$refs)
      this.$refs.form.resetFields()
    },
    login () {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.post('login', this.form)
        if (res.meta.status !== 200) return this.$message.error('登陆错误')
        this.$message.success('登陆成功')
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped lang='less'>
 .global-container {
    width: 100%;
    height: 100%;
    background: url(../assets/two.png) no-repeat center center;
    position: relative;
    .login-box {
        width: 450px;
        height: 300px;
        border-radius:5px;
        position: absolute;
        top:50%;
        left:50%;
        transform: translate(-50%,-50%);
        .avatar-box {
            width: 130px;
            height: 130px;
            border-radius: 50%;
            background:#fff;
            border: 1px solid #eee;
            padding:10px;
            position: absolute;
            left:50%;
            transform: translate(-50%,-50%);
            box-shadow: 0px 0px 10px black;
            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
            }
        }
    }
 }

 .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 15px;
    box-sizing: border-box;
    .btns {
      float: right;
    }
  }
</style>
